package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.ActivityDto;
import com.qk.entity.Activity;
import com.qk.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/activities")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 查询活动列表
     */
    @GetMapping
    public Result list(ActivityDto activitydto){
        log.info("查询活动列表");
        PageResult<Activity> pageResult = activityService.list(activitydto);
        return Result.success(pageResult);
    }

    /**
     * 删除活动
     */
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        activityService.delete(id);
        return Result.success();
    }

    /**
     * 添加活动
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Activity activity){
        log.info("添加活动");
        activityService.add(activity);
        return Result.success();
    }

    /**
     * 根据id查询活动
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询活动");
        Activity activity = activityService.getById(id);
        return Result.success(activity);
    }

    /**
     * 更新活动
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Activity activity){
        log.info("更新活动");
        activityService.update(activity);
        return Result.success();
    }

    /**
     * 根据类型查询活动
     */
    @GetMapping("/type/{type}")
    public Result getByType(@PathVariable Integer type){
        log.info("根据类型查询活动");
        List<Activity> list = activityService.getByType(type);
        return Result.success(list);
    }
}
